package de.elmar_baumann.jbirthdays.ui;

import de.elmar_baumann.jbirthdays.AppLoggingSystem;
import de.elmar_baumann.jbirthdays.ical.ExportToIcalAction;
import de.elmar_baumann.jbirthdays.imexport.QtBirthdaysDbImport;
import de.elmar_baumann.jbirthdays.imexport.XmlExportAction;
import de.elmar_baumann.jbirthdays.imexport.XmlImportAction;
import de.elmar_baumann.jbirthdays.util.Bundle;
import de.elmar_baumann.jbirthdays.util.DesktopMail;
import de.elmar_baumann.jbirthdays.util.Mnemonics;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.nio.charset.Charset;
import java.util.Collection;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;

/**
 * @author Elmar Baumann
 */
public class ToolsPanel extends javax.swing.JPanel {

    public static final String PROPERTY_IMPORTED = "imported";
    private static final long serialVersionUID = 1L;
    private final XmlImportAction xmlImportAction = new XmlImportAction(this);

    public ToolsPanel() {
        initComponents();
        postInitComponents();
    }

    private void postInitComponents() {
        Mnemonics.setMnemonics(this);
        Collection<Charset> charsets = Charset.availableCharsets().values();
        comboBoxQtImportCharsets.setModel(new DefaultComboBoxModel<>(charsets.toArray(new Charset[charsets.size()])));
        comboBoxQtImportCharsets.setSelectedItem(Charset.defaultCharset());
        xmlImportAction.addPropertyChangeListener(importedListener);
        setCharsetInfo();
    }

    private void setCharsetInfo() {
        String info = Bundle.getString(ToolsPanel.class, "ToolsPanel.Info.Charsets", "windows-1252", "UTF-8");
        labelQtImportInfoCharsets.setText(info);
    }

    private void importQtDb() {
        int importCount = QtBirthdaysDbImport.importPersons(this, (Charset) comboBoxQtImportCharsets.getSelectedItem());
        if (importCount > 0) {
            firePropertyChange(PROPERTY_IMPORTED, false, true);
        }
        JOptionPane.showMessageDialog(this,
                Bundle.getString(ToolsPanel.class, "ToolsPanel.Imported", importCount),
                Bundle.getString(ToolsPanel.class, "ToolsPanel.Imported.Title"),
                JOptionPane.INFORMATION_MESSAGE);
    }

    private void sendBugReport() {
        DesktopMail.sendEmail("eb@elmar-baumann.de",
                Bundle.getString(ToolsPanel.class, "ToolsPanel.SendBugreport.Subject"),
                Bundle.getString(ToolsPanel.class, "ToolsPanel.SendBugreport.Body"));
    }

    private final PropertyChangeListener importedListener = new PropertyChangeListener() {
        @Override
        public void propertyChange(PropertyChangeEvent evt) {
            if ("imported".equals(evt.getPropertyName())) {
                firePropertyChange(PROPERTY_IMPORTED, false, true);
            }
        }
    };

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    private void initComponents() {//GEN-BEGIN:initComponents
        java.awt.GridBagConstraints gridBagConstraints;

        panelContent = new javax.swing.JPanel();
        panelExport = new javax.swing.JPanel();
        buttonExport = new javax.swing.JButton();
        labelExportInfo = new javax.swing.JLabel();
        buttonExportToIcal = new javax.swing.JButton();
        labelIcalInfo = new javax.swing.JLabel();
        panelImport = new javax.swing.JPanel();
        buttonImport = new javax.swing.JButton();
        labelImportInfo = new javax.swing.JLabel();
        panelOldQtBirthdayApp = new javax.swing.JPanel();
        labelQtImportFileEncoding = new javax.swing.JLabel();
        comboBoxQtImportCharsets = new javax.swing.JComboBox<>();
        buttonQtImport = new javax.swing.JButton();
        labelQtImportInfoCharsets = new javax.swing.JLabel();
        buttonViewLogfile = new javax.swing.JButton();
        buttonSendBugMail = new javax.swing.JButton();
        panelFill = new javax.swing.JPanel();

        setLayout(new java.awt.GridBagLayout());

        panelContent.setLayout(new java.awt.GridBagLayout());

        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("de/elmar_baumann/jbirthdays/ui/Bundle"); // NOI18N
        panelExport.setBorder(javax.swing.BorderFactory.createTitledBorder(bundle.getString("ToolsPanel.panelExport.border.title"))); // NOI18N
        panelExport.setLayout(new java.awt.GridBagLayout());

        buttonExport.setAction(new XmlExportAction(this));
        buttonExport.setText(bundle.getString("ToolsPanel.buttonExport.text")); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 0, 5, 0);
        panelExport.add(buttonExport, gridBagConstraints);

        labelExportInfo.setText(bundle.getString("ToolsPanel.labelExportInfo.text")); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        panelExport.add(labelExportInfo, gridBagConstraints);

        buttonExportToIcal.setAction(new ExportToIcalAction(this));
        buttonExportToIcal.setText(bundle.getString("ToolsPanel.buttonExportToIcal.text")); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 0, 5, 0);
        panelExport.add(buttonExportToIcal, gridBagConstraints);

        labelIcalInfo.setText(bundle.getString("ToolsPanel.labelIcalInfo.text")); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        panelExport.add(labelIcalInfo, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(10, 10, 0, 10);
        panelContent.add(panelExport, gridBagConstraints);

        panelImport.setBorder(javax.swing.BorderFactory.createTitledBorder(bundle.getString("ToolsPanel.panelImport.border.title"))); // NOI18N
        panelImport.setLayout(new java.awt.GridBagLayout());

        buttonImport.setAction(xmlImportAction);
        buttonImport.setText(bundle.getString("ToolsPanel.buttonImport.text")); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 10, 5, 0);
        panelImport.add(buttonImport, gridBagConstraints);

        labelImportInfo.setText(bundle.getString("ToolsPanel.labelImportInfo.text")); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        panelImport.add(labelImportInfo, gridBagConstraints);

        panelOldQtBirthdayApp.setBorder(javax.swing.BorderFactory.createTitledBorder(bundle.getString("ToolsPanel.panelOldQtBirthdayApp.border.title"))); // NOI18N
        panelOldQtBirthdayApp.setLayout(new java.awt.GridBagLayout());

        labelQtImportFileEncoding.setLabelFor(comboBoxQtImportCharsets);
        labelQtImportFileEncoding.setText(bundle.getString("ToolsPanel.labelQtImportFileEncoding.text")); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 0, 0);
        panelOldQtBirthdayApp.add(labelQtImportFileEncoding, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 0, 0);
        panelOldQtBirthdayApp.add(comboBoxQtImportCharsets, gridBagConstraints);

        buttonQtImport.setText(bundle.getString("ToolsPanel.buttonQtImport.text")); // NOI18N
        buttonQtImport.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonQtImportActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 0, 0);
        panelOldQtBirthdayApp.add(buttonQtImport, gridBagConstraints);

        labelQtImportInfoCharsets.setText("Will be set programatically"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 0, 0);
        panelOldQtBirthdayApp.add(labelQtImportInfoCharsets, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        panelImport.add(panelOldQtBirthdayApp, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(10, 10, 0, 10);
        panelContent.add(panelImport, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 1.0;
        add(panelContent, gridBagConstraints);

        buttonViewLogfile.setAction(AppLoggingSystem.createViewLogfileAction());
        buttonViewLogfile.setText(bundle.getString("ToolsPanel.buttonViewLogfile.text")); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(7, 10, 0, 0);
        add(buttonViewLogfile, gridBagConstraints);

        buttonSendBugMail.setText(bundle.getString("ToolsPanel.buttonSendBugMail.text")); // NOI18N
        buttonSendBugMail.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonSendBugMailActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 10, 0, 0);
        add(buttonSendBugMail, gridBagConstraints);

        javax.swing.GroupLayout panelFillLayout = new javax.swing.GroupLayout(panelFill);
        panelFill.setLayout(panelFillLayout);
        panelFillLayout.setHorizontalGroup(
            panelFillLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );
        panelFillLayout.setVerticalGroup(
            panelFillLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.VERTICAL;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(10, 0, 0, 0);
        add(panelFill, gridBagConstraints);
    }//GEN-END:initComponents

    private void buttonQtImportActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonQtImportActionPerformed
        importQtDb();
    }//GEN-LAST:event_buttonQtImportActionPerformed

    private void buttonSendBugMailActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonSendBugMailActionPerformed
        sendBugReport();
    }//GEN-LAST:event_buttonSendBugMailActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonExport;
    private javax.swing.JButton buttonExportToIcal;
    private javax.swing.JButton buttonImport;
    private javax.swing.JButton buttonQtImport;
    private javax.swing.JButton buttonSendBugMail;
    private javax.swing.JButton buttonViewLogfile;
    private javax.swing.JComboBox<Charset> comboBoxQtImportCharsets;
    private javax.swing.JLabel labelExportInfo;
    private javax.swing.JLabel labelIcalInfo;
    private javax.swing.JLabel labelImportInfo;
    private javax.swing.JLabel labelQtImportFileEncoding;
    private javax.swing.JLabel labelQtImportInfoCharsets;
    private javax.swing.JPanel panelContent;
    private javax.swing.JPanel panelExport;
    private javax.swing.JPanel panelFill;
    private javax.swing.JPanel panelImport;
    private javax.swing.JPanel panelOldQtBirthdayApp;
    // End of variables declaration//GEN-END:variables
}
